package com.per.demobeetlsql.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.per.demobeetlsql.datasource.DynamicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Cheng
 * @date 2020/9/24 16:05
 */
@Configuration
public class DataSourceConfig {

    @Autowired
    private Environment env;

    @Value("${spring.datasource.names:}")
    private String datasourceNames;

    /**
     * 默认数据源
     *
     * @return 默认数据源
     */
    private DataSource getDefaultDataSource() {
        DruidDataSource defaultDS = new DruidDataSource();
        defaultDS.setUrl(env.getProperty("spring.datasource.url"));
        defaultDS.setUsername(env.getProperty("spring.datasource.username"));
        defaultDS.setPassword(env.getProperty("spring.datasource.password"));
        defaultDS.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        System.out.println(defaultDS);
        return defaultDS;
    }

    /**
     * 已经配置的动态数据源。
     */
    private Map<Object, Object> getDataSources() {
        Map<Object, Object> map = new HashMap<>(2);
        if (datasourceNames != null && datasourceNames.length() > 0) {
            String[] names = datasourceNames.split(",");
            for (String name : names) {
                DruidDataSource dataSource = new DruidDataSource();
                dataSource.setUrl(env.getProperty("spring.datasource." + name + ".url"));
                dataSource.setUsername(env.getProperty("spring.datasource." + name + ".username"));
                dataSource.setPassword(env.getProperty("spring.datasource." + name + ".password"));
                dataSource.setDriverClassName(env.getProperty("spring.datasource." + name + ".driver-class-name"));
                dataSource.setBreakAfterAcquireFailure(true);
                dataSource.setConnectionErrorRetryAttempts(0);
                map.put(name, dataSource);
            }
        }
        return map;
    }

    @Bean
    public DynamicDataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = DynamicDataSource.getInstance();
        Map<Object, Object> dataSources = getDataSources();
        System.out.println("datasource.size = " + dataSources.size());
        if (dataSources.size() > 0) {
            dynamicDataSource.setTargetDataSources(dataSources);
        }
        DataSource ds = getDefaultDataSource();
        if (ds != null) {
            dynamicDataSource.setDefaultTargetDataSource(ds);
            Map<Object, Object> map = new HashMap<>();
            map.put("mysql", ds);
            //map.put("sqlserver", ds);
            dynamicDataSource.setTargetDataSources(map);
        }
        return dynamicDataSource;
    }

}
